<!--
* @author dumeng
水印列表
-->
<template>
  <div class="courseware-container">
    <el-collapse-transition>
      <div v-show="searchshow" class="courseware-search merge">
        <span class="svg-container">
          <svg-icon icon-class="search" />
        </span>
        <el-input v-model="listQuery.watermarkName" placeholder="请输入模板名称搜索" class="search-input" @keyup.enter.native="handleFilter" />
        <div class="courseware-search-btn">
          <el-button class="filter-item" type="primary" @click="handleFilter">搜索</el-button>
          <el-button class="filter-item" type="primary" @click="resettingBtn">重置</el-button>
          <span class="courseware-search-more" @click="searchshow = !searchshow">
            更多筛选条件
            <i class="el-icon-arrow-down" />
          </span>
        </div>
      </div>
    </el-collapse-transition>
    <el-collapse-transition>
      <div v-show="!searchshow" class="courseware-search separate">
        <div class="courseware-search-inner">
          <el-row>
            <el-col :span="8">
              <div class="search-inner-item">
                <div class="item-label">模板名称</div>
                <el-input v-model.trim="search.watermarkName" style="width: 80%;" class="filter-item" placeholder="请输入水印名称搜索" />
              </div>
            </el-col>
            <!-- <el-col :span="8">
              <div class="search-inner-item">
                <div class="item-label">使用场景</div>
                <el-select v-model="search.watermarkType" placeholder="请选择使用场景搜索">
                  <el-option v-for="item in dict['SYCJ']" :key="item.dictValue" :label="item.dictName" :value="item.dictValue" />
                </el-select>
              </div>
            </el-col> -->
            <el-col :span="8">
              <div class="search-inner-item">
                <div class="item-label">水印状态</div>
                <el-radio-group v-model="search.status">
                  <el-radio label="ZT01">启用</el-radio>
                  <el-radio label="ZT02">禁用</el-radio>
                </el-radio-group>
              </div>
            </el-col>
          </el-row>
        </div>
        <div class="courseware-search-btn">
          <el-button type="primary" @click="handleFilter">搜索</el-button>
          <el-button class="filter-item" type="primary" @click="resettingBtn">重置</el-button>
          <span class="courseware-search-more" @click="searchshow = !searchshow">
            精简筛选条件
            <i class="el-icon-arrow-up" />
          </span>
        </div>
      </div>
    </el-collapse-transition>
    <div v-if="!isSelect" class="courseware-btn">
      <div class="btn-title">
        <span>水印管理</span>
      </div>
      <div class="btn-btns">
        <el-button style="margin-left: 10px;" @click=";(coursewareInfo = {}), (isCreate = true)">新建水印 </el-button>
        <el-button style="margin-left: 10px;" @click="handleSelectDelet">批量删除 </el-button>
        <!-- <el-button style="margin-left: 10px;" @click="handleSelectSend">批量发布 </el-button> -->
      </div>
    </div>
    <div class="courseware-list">
      <v-table
        :list="courseList"
        tag="watermarkLibrary"
        :list-loading="listLoading"
        :filter-column="filtercolumn"
        :source-arr="sourceArr"
        @handleSelectionChange="handleSelectionChange"
      >
        <!-- <el-table-column prop="transcodeStatusValue" label="启用状态">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              active-text="启用"
              inactive-text="禁用"
              active-value="ZT01"
              inactive-value="ZT02"
              @change="handleStatus(scope.row)"
            />
          </template>
        </el-table-column> -->
        <el-table-column v-if="!isSelect" fixed="right" label="操作" width="130">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="handleDefault(scope.row)">
              {{ scope.row.isDefault === 1 ? '取消默认水印' : '设为默认水印' }}
            </el-button>
            <el-button type="text" size="small" @click="handleSee(scope.row)">查看</el-button>
            <el-button type="text" size="small" @click="handleUpdate(scope.row)">编辑</el-button>
            <el-button
              v-if="scope.row.status !== 'KCZT01' && scope.row.transcodeStatus !== '2'"
              type="text"
              size="small"
              @click="handleDel(scope.row)"
            >
              删除
            </el-button>
            <el-button v-if="scope.row.status === 'ZT02'" type="text" size="small" @click="handleStatus(scope.row.id, 'ZT01')">启用</el-button>
            <el-button v-else type="text" size="small" @click="handleStatus(scope.row.id, 'ZT02')">禁用</el-button>
          </template>
        </el-table-column>
      </v-table>
    </div>
    <pagination v-show="total > 0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    <!--    -->
    <el-dialog
      v-if="preview.state"
      v-el-drag-dialog
      :visible.sync="preview.state"
      :close-on-click-modal="false"
      :fullscreen="isFull"
      title="预览"
      center
      width="600px"
      @close="preview.stat = false"
    >
      <span slot="title" class="my-title">
        <span>预览</span>
        <div v-if="preview.fileType === 'KCLX02'" class="full">
          <el-button size="small" type="primary" icon="el-icon-full-screen" @click="isFull = !isFull">{{ isFull ? '取消全屏' : '全屏' }}</el-button>
        </div>
      </span>
      <courseware-preview
        v-if="preview.state"
        :is-full="isFull"
        :type="preview.fileType"
        :show-type="preview.showType"
        :file-id="preview.fileId"
        style="width:100%;height:100%;"
      />
    </el-dialog>
    <create-courseware v-if="isCreate" :info="coursewareInfo" @close="closeFun" />
    <el-dialog v-el-drag-dialog :close-on-click-modal="false" :destroy-on-close="true" :visible.sync="visibleState" title="查看" center width="800px">
      <el-row class="see-dialog">
        <el-col :span="12">
          <div class="box">
            <div class="key">模板名称：</div>
            <div class="value">{{ coursewareInfo.watermarkName || '' }}</div>
          </div>
        </el-col>
        <el-col :span="12">
          <div class="box">
            <div class="key">模板图标：</div>
            <div class="value">
              <el-image :src="coursewareInfo.watermarkIconUrl" fit="contain" />
            </div>
          </div>
        </el-col>
        <el-col :span="12">
          <div class="box">
            <div class="key">位置：</div>
            <div class="value">{{ coursewareInfo.watermarkSide || '' }}</div>
          </div>
        </el-col>
        <!-- <el-col :span="12">
          <div class="box">
            <div class="key">方向：</div>
            <div class="value">{{ coursewareInfo.direction || '' }}</div>
          </div>
        </el-col> -->
        <el-col :span="12">
          <div class="box">
            <div class="key">模板名称：</div>
            <div class="value">{{ coursewareInfo.watermarkName || '' }}</div>
          </div>
        </el-col>
        <el-col :span="12">
          <div class="box">
            <div class="key">纵向间隔：</div>
            <div class="value">{{ coursewareInfo.longitudinalSpacing || '' }}</div>
          </div>
        </el-col>
        <el-col :span="12">
          <div class="box">
            <div class="key">横向间隔：</div>
            <div class="value">{{ coursewareInfo.horizontalSpacing || '' }}</div>
          </div>
        </el-col>
        <!-- <el-col :span="12">
          <div class="box">
            <div class="key">使用场景：</div>
            <div class="value">{{ coursewareInfo.watermarkTypeValue || '' }}</div>
          </div>
        </el-col> -->
        <el-col :span="12">
          <div class="box">
            <div class="key">是否启用：</div>
            <div class="value">{{ coursewareInfo.statusValue || '' }}</div>
          </div>
        </el-col>
      </el-row>
      <section slot="footer" class="dialog-footer">
        <el-button style="padding: 10px 30px;" @click="visibleState = false">关 闭</el-button>
      </section>
    </el-dialog>
  </div>
</template>
<script>
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import { deleteSubject, fetchList, get, batchPublish, batchDelet, modifyState, waterDefault } from '../api/videoLibrary'
import { getDictByCode } from '../api/createCourseware'
import { mapGetters } from 'vuex'
import elDragDialog from '@/directive/el-drag-dialog' // base on element-ui
import CoursewarePreview from '../components/CoursewarePreview'
import CreateCourseware from '../components/createCourseware'
import vTable from '@/components/Table'
export default {
  name: 'VideoLibrary',
  components: { Pagination, CoursewarePreview, CreateCourseware, vTable },
  directives: { elDragDialog },
  props: {
    isSelect: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      divt: [],
      isFull: false, // 是否全屏
      listLoading: false,
      list: null,
      total: 0,
      searchshow: true,
      search: {
        watermarkName: '',
        // watermarkType: '',
        status: ''
      },
      classifyProps: {
        label: 'classifyName',
        value: 'id',
        checkStrictly: true
      },
      classifyOptions: [],
      statusOptions: [
        {
          statusName: '全部',
          id: '1'
        },
        {
          statusName: '转码成功/未发布',
          id: '2'
        },
        {
          statusName: '已发布',
          id: '3'
        },
        {
          statusName: '转码中',
          id: '4'
        },
        {
          statusName: '转码失败',
          id: '5'
        }
      ],
      activeTab: '1',
      listQuery: {
        page: 1,
        limit: 10,
        watermarkName: ''
      },
      checkList: [],
      courseList: [],
      view: '/API-DOCUMENT/document/file/download?id=',
      dict: {
        KCLX: [] // 水印类型
      },
      preview: {
        fileId: '',
        fileType: '',
        showType: '',
        state: false
      },
      coursewareInfo: {},
      visibleState: false,
      multipleSelection: [],
      isCreate: false,
      isOss: sessionStorage.getItem('FILE_SAVE_THIRD_CONFIG') || false,
      sourceArr: ['watermarkIconUrl'],
      isCreatePage: true
    }
  },
  computed: {
    ...mapGetters(['sidebar'])
  },
  created() {
    this.activeTab = this.$route.query.active || '1'
    this.getList()
    this.getDict('SYCJ')
  },
  // activated() {
  //   if (this.isCreatePage) { this.listQuery.page = 1 }
  //   this.getList()
  // },
  methods: {
    // 设置默认水印
    handleDefault(row) {
      const data = {
        id: row.id,
        // watermarkType: row.watermarkType,
        isDefault: row.isDefault === 0 ? 1 : 0
      }
      const loading = this.$loading({
        lock: true,
        text: '请稍后',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.3)'
      })
      return new Promise((resolve, reject) => {
        waterDefault(data)
          .then((response) => {
            loading.close()
            this.$message({
              title: '成功',
              message: '设置成功',
              type: 'success',
              duration: 2000
            })
            this.getList()
          })
          .catch((error) => {
            loading.close()
            reject(error)
          })
      })
    },
    // 修改水印状态
    handleStatus(id, status) {
      const data = {
        id,
        status: status
      }
      const loading = this.$loading({
        lock: true,
        text: '请稍后',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.3)'
      })
      return new Promise((resolve, reject) => {
        modifyState(data)
          .then((response) => {
            loading.close()
            this.$message({
              title: '成功',
              message: '修改成功',
              type: 'success',
              duration: 2000
            })
            this.getList()
          })
          .catch((error) => {
            loading.close()
            reject(error)
          })
      })
    },
    refresh(index, row) {
      this.getData(row.id).then((res) => {
        if (res !== 0) {
          this.$set(this.courseList, index, res)
        }
      })
    },
    getData(id) {
      const loading = this.$loading({
        lock: true,
        text: '请稍后',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.3)'
      })
      return new Promise((resolve, reject) => {
        get({ id: id })
          .then((res) => {
            loading.close()
            resolve(res.data.data)
          })
          .catch((e) => {
            loading.close()
            resolve(0)
          })
      })
    },
    closeFun() {
      this.isCreate = false
      this.getList()
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
      this.$emit('handleSelect', this.multipleSelection)
    },
    /**
     * 查看
     * i: 水印信息
     */
    handleSee(i) {
      this.coursewareInfo = i
      this.visibleState = true
    },
    getList(obj) {
      this.listLoading = true

      var query = {
        page: obj ? obj.page : this.listQuery.page,
        pageSize: obj ? obj.limit : this.listQuery.limit,
        type: this.isSelect ? '3' : this.activeTab
      }
      if (this.searchshow) {
        query['watermarkName'] = this.listQuery.watermarkName
      } else {
        query['watermarkName'] = this.search.watermarkName
        // query['watermarkType'] = this.search.watermarkType
        query['status'] = this.search.status
      }
      return new Promise((resolve, reject) => {
        fetchList(query)
          .then((response) => {
            this.courseList = response.data.data.rows
            this.total = response.data.data.total
            this.listLoading = false
          })
          .catch((error) => {
            this.listLoading = false
            reject(error)
          })
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    resettingBtn() {
      this.listQuery.watermarkName = ''
      for (const i in this.search) {
        this.search['' + i + ''] = ''
      }
      this.listQuery.page = 1
      this.getList()
    },
    handleUpdate(item) {
      this.isCreatePage = false
      this.$router.push({ path: this.$filterPath('videoWatermarkSetManage'), query: { i: item.id, type: 'u', active: this.activeTab } })
    },
    handleDel(row) {
      if (row.status === 'KCZT01') {
        this.$message({
          message: '水印已发布，无法删除',
          type: 'warning'
        })
      } else {
        this.deleteSubject(row.id)
      }
    },
    deleteSubject(ids) {
      this.$confirm('此操作将永久删除该水印, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          const loading = this.$loading({
            lock: true,
            text: '请稍后',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.3)'
          })
          return new Promise((resolve, reject) => {
            deleteSubject({ ids: ids })
              .then(() => {
                loading.close()
                this.$message({
                  title: '成功',
                  message: '删除成功',
                  type: 'success',
                  duration: 2000
                })
                this.getList()
              })
              .catch((error) => {
                loading.close()
                reject(error)
              })
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    filtercolumn(scope, item) {
      return scope.row[item]
    },
    handleSelectDelet() {
      if (this.multipleSelection.length > 0) {
        this.$confirm('此操作将永久删除选中水印, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            const loading = this.$loading({
              lock: true,
              text: '请稍后',
              spinner: 'el-icon-loading',
              background: 'rgba(0, 0, 0, 0.3)'
            })
            const params = {
              ids: this.multipleSelection.map((i) => i.id).join(',')
            }
            batchDelet(params)
              .then((res) => {
                loading.close()
                this.getList()
              })
              .catch((err) => loading.close(err))
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            })
          })
      } else {
        this.$message({
          type: 'warning',
          message: '请选择需要删除的水印'
        })
      }
    },
    handleSelectSend() {
      if (this.multipleSelection.length > 0) {
        const loading = this.$loading({
          lock: true,
          text: '请稍后',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.3)'
        })
        const params = {
          id: this.multipleSelection.map((i) => i.id).join(','),
          status: 'KCZT01'
        }
        batchPublish(params)
          .then((res) => {
            loading.close()
            this.getList()
          })
          .catch((err) => loading.close(err))
      } else {
        this.$message({
          type: 'warning',
          message: '请选择需要发布的水印'
        })
      }
    },
    /**
     * 获取字典
     */
    getDict(code) {
      return new Promise((resolve, reject) => {
        getDictByCode(code)
          .then((res) => {
            const datas = res.data.data && res.data.data.length > 0 ? res.data.data[0].list : []
            this.$set(this.dict, code, datas)
            this.$forceUpdate()
          })
          .catch((error) => {
            reject(error)
          })
      })
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss">
$dark_gray: #bfced6;
.courseware-container {
  padding: 20px;
  .el-button + .el-button {
    margin-left: 0 !important;
  }
  .my-title {
    width: 100%;
    display: inline-block;
    font-size: 16px !important;
    font-family: PingFangSC-Medium !important;
    font-weight: 700 !important;
    color: #333333 !important;
    position: relative;
    .full {
      position: absolute;
      width: 100px;
      height: 58px;
      right: 50px;
      top: -18px;
      display: flex;
      align-items: center;
      text-align: right;
    }
  }
  .courseware-search {
    background-color: #fff;
    padding: 0 20px;

    .svg-container {
      padding: 0 5px 0 10px;
      color: $dark_gray;
      vertical-align: middle;
      display: inline-block;

      &_login {
        font-size: 20px;
      }
    }

    .search-input {
      width: 60%;
      position: relative;

      &:after {
        content: '';
        position: absolute;
        left: -25px;
        right: 0;
        bottom: 8px;
        height: 1px;
        background: rgba(226, 227, 232, 1);
      }

      input {
        outline: none;
        background: transparent;
        border: none;
        outline: medium;
        padding: 0;

        &:focus {
          outline: none;
          background-color: transparent;
        }
      }
    }

    .courseware-search-btn {
      display: inline-block;
      float: right;

      .el-button.is-round {
        padding: 10px 30px;
        margin-right: 34px;
      }

      .courseware-search-more {
        font-size: 14px;
        font-weight: 500;
        @include font-color($mainColor);
        cursor: pointer;
      }
    }

    &.merge {
      height: 50px;
      line-height: 50px;
    }

    &.separate {
      .courseware-search-inner {
        padding-top: 10px;

        .search-inner-item {
          .item-label {
            height: 30px;
            line-height: 30px;
            font-size: 14px;
            color: #a6a6a6;
            font-weight: 400;
          }

          .el-input {
            position: relative;

            .el-input__inner {
              border: none;
              padding: 0;
            }

            &:after {
              content: '';
              height: 1px;
              background-color: #e2e3e8;
              position: absolute;
              left: 0;
              right: 0;
              bottom: 0;
            }
          }
        }
      }

      .courseware-search-btn {
        text-align: center;
        display: block;
        float: none;
        height: 56px;
        line-height: 56px;
      }
    }
  }

  .classify-tab {
    width: 100%;
    margin: 10px 0;
    padding: 0 10px;
    background-color: #fff;
    display: flex;
    border-radius: 4px;

    .el-tabs__nav-wrap::after {
      display: none;
    }

    .el-tabs__header {
      margin: 0;
    }
  }

  .courseware-btn {
    display: flex;
    height: 46px;
    line-height: 46px;
    width: 100%;

    .btn-title {
      flex: 1;
      font-size: 16px;
      font-weight: bold;
      color: rgba(51, 51, 51, 1);
    }
  }

  .courseware-list {
    background-color: white;
    margin-top: 10px;
    padding: 10px;
    .el-image {
      width: 100%;
      height: auto;
      max-height: 140px;
      .image-slot {
        width: 100%;
        height: 67px;
        background-color: #f8f8f8;
        display: flex;
        align-items: center;
        justify-content: center;
      }
    }
    .el-form-item {
      margin-bottom: 5px;
    }

    .el-dropdown-link {
      cursor: pointer;
      @include font-color($mainColor);
    }
  }

  .pagination-container {
    background: none;
  }
  .el-row.see-dialog {
    /* height: 500px; */
    overflow-y: auto;
    padding: 5px 40px;
    .box {
      display: flex;
      width: 100%;
      min-height: 38px;
      .key {
        width: 70px;
        &.max {
          width: 100px;
        }
        &.min {
          width: 50px;
        }
      }
      .value {
        flex: 1;
        color: #333;
        font-weight: 500;
        padding-right: 30px;

        &.textarea-view {
          white-space: pre-line;
          border: 1px solid #e5e5e5;
          border-radius: 6px;
          padding: 10px;
          color: #333;
          background-color: #f9f9f9;
          font-weight: normal;
        }
      }
    }
    .el-col {
      padding-top: 10px;
      margin-top: 15px;
    }
  }
  .cell {
    .el-image {
      width: 100%;
    }
  }
}
</style>
